package tech.aistar.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.Register;
import tech.aistar.entity.Result;
import tech.aistar.service.IRegisterService;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/register")
public class RegisterController {


    @Autowired
    private IRegisterService registerService;

   @RequestMapping("/check")
    public String register(){
       return "/login/register";
   }

   @PostMapping("/adduser")
    public String addUser(@RequestParam("account")String account,@RequestParam("password")String password,@RequestParam("password")String password2, @RequestParam("identity")String identity){
        Register register = new Register();

        register.setAccount(account);

        if (password.equals(password2)){
           register.setPassword(password);
        }else {
           return "/login/register";
        }

        if(identity.equals("经纪人")) {
            register.setIdentity("2");
        }
        else if(identity.equals("租客")) {
            register.setIdentity("1");
        }
        else if(identity.equals("房东")){
            register.setIdentity("3");
        }

        registerService.insert(register);

        return "/login/login";
   }


    @PostMapping("/checkaccount")
    public @ResponseBody Result select(String account) {
        Register register = registerService.selectByAccount(account);
        if (register == null) {
            return new Result("200", "修改成功");
        } else {
            return new Result("500", "修改失败");
        }
    }


}
